﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using DATA_operate_;
using System.Text;



using System.Collections.Generic;





public partial class design_page_teacher_Default : System.Web.UI.Page
{
    DATA_operate publishinfo = new DATA_operate();
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            HttpCookie cookie = Request.Cookies["teaid"];
            string teacherid = cookie.Value.ToString();             //教师ID
            string sql = "select _Cname from _class where _Cid in (select _Cid from _teacher_class where _Tid='" + teacherid + "')";
            publishinfo.Data_bind(sql, DropDownList1, "_Cname");
        }
    }
   
    protected void Button2_Click(object sender, EventArgs e)
    {
        if(TextBox2.Text=="")
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "right", "window.alert('作业名称不能为空');", true);

        }
        else if(TextBox4.Text=="")
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "right", "window.alert('作业内容不能为空');", true);

        }
        else
        {
            HttpCookie cookie = Request.Cookies["teaid"];
            string homeworkid = Make_homeworkid();                   //作业ID号
            string teacherid = cookie.Value.ToString();             //教师ID
            string Hname = this.TextBox2.Text.ToString();          //作业名称
            string Hupdatime = DateTime.Now.ToLocalTime().ToString();  //发布时间
            string Hcontent = this.TextBox4.Text.ToString();     //作业内容


            string classid = publishinfo.dataread("_class", "_Cid", "_Cname", DropDownList1.SelectedItem.Text);

            if (TextBox3.Text == "")//没有提供参考答案
            {
                publishinfo.dataadd("_main_homework", "_mHid,_mHTid,_mHname,_mHupdatetime,_mHContent", homeworkid + "," + teacherid + "," + Hname + "," + Hupdatime + "," + Hcontent);//添加如作业主表
            }
            else
                publishinfo.dataadd("_main_homework", "_mHid,_mHTid,_mHname,_mHupdatetime,_mHContent,_mTrueanswer", homeworkid + "," + teacherid + "," + Hname + "," + Hupdatime + "," + Hcontent + "," + TextBox3.Text);//添加如作业主表


            publishinfo.dataadd("_class_homework", classid + "," + homeworkid);//添加班级作业表

            string read_sql = "select _Sid from _student where _SCid= '" + classid + "'"; //格式化数据修改语句
            SqlCommand con = publishinfo.sqlcmd(read_sql);
            SqlDataReader dr = con.ExecuteReader();
            string str = "";
            while (dr.Read())
            {
                str = dr["_Sid"] + "";    //获得字段
                publishinfo.dataadd("_student_homework", "_Sid,_Hid", str + "," + homeworkid);//添加班级作业表
            }
            Page.ClientScript.RegisterStartupScript(this.GetType(), "right", "window.alert('作业发布成功');", true);
        }
       

    }


 

  

    protected void Button3_Click(object sender, EventArgs e)    //重置
    {

        TextBox2.Text = "";
        TextBox4.Text = "";
        TextBox3.Text = "";

      


    }


    private string Make_homeworkid()   //生成作业ID
    {

        string numA = publishinfo.dataread("MAX(_mHid)", "_main_homework", true);   //获得数据库最大作业ID 号
        if(numA==null)
        {
            numA = "000000000000000";
        }
        string numB = "1";   //最大ID 号加1

        int lenA = 0, lenB = 0;
        int lenI = 0, intTen = 0;
        int intA = 0, intB = 0;

        StringBuilder result = new StringBuilder("");
        lenA = numA.Length;
        lenB = numB.Length;
        if (lenA > lenB)
            lenI = lenA;
        else
            lenI = lenB;
        for (int i = 0; i < lenI; i++)
        {
            if (i < lenA)
                intA = Convert.ToInt16(numA.Substring(lenA - i - 1, 1));
            else
                intA = 0;
            if (i < lenB)
                intB = Convert.ToInt16(numB.Substring(lenB - i - 1, 1));
            else
                intB = 0;
            int temp = intA + intB + intTen;
            result.Insert(0, Convert.ToString((int)temp % 10));
            intTen = (int)temp / 10;
        }
        if (intTen > 0)
            result.Insert(0, Convert.ToString(intTen));
        return result.ToString();
    }




    protected void Button4_Click(object sender, EventArgs e)    //返回
    {
        Server.Transfer("teacher_index.aspx");
    }
}